package cz.jm.web.jmlib.client.base;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;

import cz.jm.web.jmlib.client.resources.Resources;

/** Zobrazenie loadovania pri nedostupnosti ostatnych casti stranky. */
public class ModalLoadingPanel {
	
	private static final ModalLoadingPanel instance = new ModalLoadingPanel();
	
	/** Vyskakovaci panel, ktory zabezpecuje modalitu. Bohuzial ma stale pozadie 
	 * a nie pekne aby mal v sebe animaciu loadovania tj. je bez obsahu.  */
	private final PopupPanel popup = new PopupPanel(false, true);
	/** Vyventrovanie a zobrazenie loadovacieho obrazku. */
	private final DockPanel centeredPanel = new DockPanel();
	/** Obrazok znazornujuci priebeh loadovania. */
	private final Image workingImg = new Image(Resources.INSTANCE.getWorking());
	
	public ModalLoadingPanel() {
		popup.getElement().getStyle().setBorderWidth(0, Unit.PX);
		// Nema nyt vidiet lebo nema obsah.
		popup.getElement().getStyle().setOpacity(0);
		// Zobrazenie zasivenia stranky.
		popup.setGlassEnabled(true);

		centeredPanel.add(workingImg, DockPanel.CENTER);
		centeredPanel.setCellHorizontalAlignment(workingImg, HasHorizontalAlignment.ALIGN_CENTER);
		centeredPanel.setCellVerticalAlignment(workingImg, HasVerticalAlignment.ALIGN_MIDDLE);
		
		hide();
    }
	
	public void show() {
		popup.center();
		popup.show();
		// Zobrazenie obsahu samostatne pretoze PopupPanel tam stale rval pozadie a nebolo to pekne.
		RootLayoutPanel.get().add(centeredPanel);
		// Panelu je potrebne vzdy nastavit size aby sa resizol podla noveho rodica. 
		centeredPanel.setSize("100%", "100%");
	}
	

	
	public void hide() {
		popup.hide();
		RootLayoutPanel.get().remove(centeredPanel);
	}
	
	public static ModalLoadingPanel getInstance() {
		return instance;
	}
	
}
